// 插件指令：
//
// MessageSpeed：强制对话速度
// 例如：（默认速度是3）
//    MessageSpeed 3
// 
// MessageResetSpeed：重置对话速度（系统界面所设置的）


let _Game_Interpreter_pluginCommand =
  Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function (command, args) {
  _Game_Interpreter_pluginCommand.call(this, command, args);
  if (command === 'MessageSpeed') $gameSystem._messageforcedSpeed = Number(args[0]);
  if (command === 'MessageResetSpeed') $gameSystem._messageforcedSpeed = null;
};

// 禁用对话加速时，同时连带禁用地图事件的加速
Scene_Map.prototype.updateMainMultiply = function() {
  this.updateMain();
  if (this.isFastForward() && $gameSystem.isFastFowardEnabled()) {
      this.updateMain();
  }
};

//=============================================================================
// Window_Message
//=============================================================================
let _Window_Message_newPage = Window_Message.prototype.newPage;
Window_Message.prototype.newPage = function(textState) {
  _Window_Message_newPage.call(this, textState);
  this._textDelay = 0;
};

let _Window_Message_updateShowFast =
  Window_Message.prototype.updateShowFast;
Window_Message.prototype.updateShowFast = function() {
  if (this.isFastForward()) this._showFast = true;
  // 如果快进键（向下翻页）被禁用，则触摸快进也要禁用。
  if (this.isTriggered() && $gameSystem.isFastFowardEnabled())
    this._showFast = true;

  if (this.messageSpeed() < 0) this._showFast = true;

};

Window_Message.prototype.messageSpeed = function() {
  const speed = !!$gameSystem._messageforcedSpeed ? $gameSystem._messageforcedSpeed : ConfigManager.messageSpeed
  return 7 - 2 * (speed.clamp(0, 4));
  //return 10 - (ConfigManager.messageSpeed.clamp(0, 11));
};

Window_Message.prototype.updateMessage = function() {
  if (this._textState) {
    while (!this.isEndOfText(this._textState)) {
      if (this.needsNewPage(this._textState)) {
          this.newPage(this._textState);
      }
      this.updateShowFast();
      if (this._textDelay <= 0 || this._showFast || this._lineShowFast) {
        this.processCharacter(this._textState);
        this._textDelay = this.messageSpeed();
      } else {
        this._textDelay -= 1;
        break;
      }
      if (!this._showFast && !this._lineShowFast) {
        break;
      }
      if (this.pause || this._waitCount > 0) {
        break;
      }
    }
    if (this.isEndOfText(this._textState)) {
      this.onEndOfText();
    }
    return true;
  } else {
    return false;
  }
};

if (Imported.YEP_X_ExtMesPack1) {

Yanfly.EMP1.Window_Message_updateMessage =
  Window_Message.prototype.updateMessage;
Window_Message.prototype.updateMessage = function() {
  var state = Yanfly.EMP1.Window_Message_updateMessage.call(this);
  if (state) {
    this._soundCount = this._soundCount || 0;
    if (this._soundCount-- <= 0) {
      this._soundCount = $gameSystem.messageSoundInterval();
      if ($gameSystem.isMessageSoundEnabled()) SoundManager.playMessageSound();
    }
  }
  return state;
}

}; // Imported.YEP_X_ExtMesPack1

/*
// 不记得为什么写了这一段了。写上后，在速度最快 且 有“\^”符时 会完全无等待时间，看不到对话，注释掉吧。
// 可能是从YEP_Options里抄的
let _Window_Message_updateWait =
  Window_Message.prototype.updateWait;
Window_Message.prototype.updateWait = function() {
  if (this.messageSpeed() < 0) {
    this._waitCount = 0;
    this._lineShowFast = true;
    this._showFast = true;
  }
  return _Window_Message_updateWait.call(this);
};
*/